<?php
/**
 * This example file is for creating a new client.
 * 
 * @author StudioCloud International Inc.
 * @version 1.0
 * @copyright Copyright 2010, StudioCloud International Inc.
 * @package Example
 * 
 */ 
 
$resultCode = 0;
$resultMessage = "";
//Check to see if any POST variables were passed and if the appropriate button was clicked
if ($_POST && $action="Submit") {
	
	//This is your apikey that can be retreived from the StudioCloud 3.0 program
	$apikey = "ENTER_APIKEY_HERE";
	
	//This is your apikeynumber that can be retreived from the StudioCloud 3.0 program
	$apikeynumber = "ENTER_APIKEYNUMBER_HERE";
		
	//This is the actual function that you want to call.  In this case, ping is the test method to make sure everything is working.
	$method = "submitContactUs";
	
	//The format that you want the response in.  The default is xml but in this case you want json
	$format = "json";
	
	//The url including all of the variables
	list($firstName, $lastName) = split(" ", $_POST['name'], 2);
	
	//Save the message to the notes field as well for safe-keeping
	$notes = $_POST["subject"] . "\n\n" . $_POST["message"];
	
	/*
	 * Send all of the data to the service using a GET method
	 * 
	 * Be sure to use urlencode function to make sure that spaces 
	 * and other characters don't cause a problem
	 */
    $url = "https://studiocloud.org/platform/Client.php?" .
    		"method=" . $method . "&" .
    		"format=" . $format . "&" .
    		"apikey=" . $apikey . "&" .
    		"apikeynumber=" . $apikeynumber . "&" .
    		"firstName=" . urlencode($firstName) . "&" .
    		"lastName=" . urlencode($lastName) . "&" .
    		"homeNumber=" . urlencode($_POST["homePhone"]) . "&" .
    		"workNumber=" . urlencode($_POST["workPhone"]) . "&" .
    		"cellNumber=" . urlencode($_POST["cellPhone"]) . "&" .
    		"street=" . urlencode($_POST["street"]) . "&" .
    		"city=" . urlencode($_POST["city"]) . "&" .
    		"state=" . urlencode($_POST["state"]) . "&" .
    		"zip=" . urlencode($_POST["zip"]) . "&" .
    		"email=" . urlencode($_POST["email"]) . "&" .
    		"notes=" . urlencode($notes) . "&" .
    		"isProspective=true&" .
    		"subject=" . urlencode($_POST["subject"]) . "&" .
    		"message=" . urlencode($_POST["message"]) . "&" .
    		"sendEmail=true";

	//Utilizes the Client URL Library (CURL). Please see http://us3.php.net/manual/en/book.curl.php
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,$url);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);

	//If there is a result then store the code and message returned
    if (!empty($buffer)) {
    	//Uses the json_decode function.  Please see http://us3.php.net/manual/en/function.json-decode.php
        $obj = json_decode($buffer);
        $resultCode = $obj->code;
        $resultMessage = $obj->message;
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
	<head>
		<title>Contact Us</title>
	</head>
	<body>
		<?php
			if ($_POST && $action="Submit") {
				if ($resultCode == 0) {
					echo "Thank you for submitting your inquiry.  We will get back to you as soon as possible.";
				}
				else {
					echo "<font color='red'>" . $resultMessage . "</font>";
				}
				echo "<br/><br/>";
			}
		?>
		<div>Welcome!  Thanks for visiting our site.  
		We love to hear from our customers so please 
		fill out the form below and we will get back 
		to you as quickly as we can.</div>
	    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	    	<table>
	    		<tr>
	    			<td colspan="2" align="center"
				    	<b>Contact Information</b>
				    </td>
				</tr>
	    		<tr>
	    			<td>Name: </td>
	    			<td><input type="text" name="name"/></td>
	    		</tr>
	    		<tr>
	    			<td>Email: </td>
	    			<td><input type="text" name="email"/></td>
	    		</tr>
	    		<tr>
	    			<td>Home Phone: </td>
	    			<td><input type="text" name="homePhone"/></td>
	    		</tr>
	    		</tr>
	    		<tr>
	    			<td>Cell Phone: </td>
	    			<td><input type="text" name="cellPhone"/></td>
	    		</tr>
	    		</tr>
	    		<tr>
	    			<td>Work Phone: </td>
	    			<td><input type="text" name="workPhone"/></td>
	    		</tr>
	    		</tr>
	    		<tr>
	    			<td>Street: </td>
	    			<td><input type="text" name="street"/></td>
	    		</tr>
	    		</tr>
	    		<tr>
	    			<td>City: </td>
	    			<td><input type="text" name="city"/></td>
	    		</tr>
	    		</tr>
	    		<tr>
	    			<td>State: </td>
	    			<td><input type="text" name="state"/></td>
	    		</tr>
	    		</tr>
	    		<tr>
	    			<td>Zip: </td>
	    			<td><input type="text" name="zip"/></td>
	    		</tr>
	    		<tr>
	    			<td colspan="2" align="center"
				    	<b>Details</b>
				    </td>
				</tr>
	    		<tr>
	    			<td>Subject: </td>
	    			<td><input type="text" name="subject"/></td>
	    		</tr>
	    		<tr>
	    			<td>Message: </td>
	    			<td><textarea name="message"></textarea></td>
	    		</tr>
	    		<tr>
	    			<td colspan="2" align="center">
	                    <input type="submit" name="action" value="Submit"/>
	                </td>
                </tr>
            </table>
	    </form>
	</body>
</html>